<?php
/*******************************************************************************
 * $Date:: Jan 7, 2011 9:38:39 PM                                              $
 * $Revision:                                                                  $
 * $Author:: Tuan Duong                                                        $
 * $Comment::                                                                  $
 ******************************************************************************/ 
/**
 * Description here...
 * 
 * @package Lua
 * @subpackage ##Subpackage##
 * @author Tuan Duong <bacduong[at]gmail[dot]com>
 * @version 
 */

class Lua_Uri extends Zend_Uri_Http
{
    /**
     * @var Lua_Uri
     */
    private static $instance = null;
    
    
    protected function __construct()
    {
        parent::__construct('http', '//' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
        // Singleton
    }
    
    /**
     * @return Lua_Uri
     */
    public static function getInstance()
    {
        if (null == self::$instance) {
            self::$instance = new Lua_Uri();
            Lua_Registry::set(Lua_Constant_General::LUA_URI, self::$instance);
        }
        return self::$instance;
    }
    
    /**
     * Get param from GET/POST request
     * @param string $param
     * @param mixed $defaultValue
     * @return mixed
     */
    public function getRequest($param, $defaultValue = null)
    {
        $params = $_REQUEST;
        if (isset($params[$param])) {
            return $params[$param];
        } else {
            return $defaultValue;
        }
    }
    
    /**
     * Get param from GET request
     * @param string $param
     * @param mixed $defaultValue
     * @return mixed
     */
    public function getRequestGet($param, $defaultValue = null)
    {
        $params = $this->getQueryAsArray();
        if (isset($params[$param])) {
            return $params[$param];
        } else {
            return $defaultValue;
        }
    }

    /**
     * Get param from POST request
     * @param string $param
     * @param mixed $defaultValue
     * @return mixed
     */
    public function getRequestPost($param, $defaultValue = null)
    {
        $params = $_POST;
        if (isset($params[$param])) {
            return $params[$param];
        } else {
            return $defaultValue;
        }
    }
    
    /**
     * Get param from SERVER variable
     * @param string $param
     * @param mixed $defaultValue
     * @return mixed
     */
    public function getServer($param, $defaultValue = '')
    {
        if (isset($_SERVER[$param])) {
            return $_SERVER[$param];
        } else {
            return $defaultValue;
        }
    }
    
    public function getParams()
    {
        return $_GET;
    }
}